package com.zerui.manager.deviceTouch.domainservice;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.manager.deviceTouch.DeviceTouch;
import com.zerui.manager.deviceTouch.gateway.DeviceTouchGateWay;
import com.zerui.manager.deviceTouchSync.DeviceTouchSync;
import com.zerui.manager.web.cmd.DeviceTouchQryCmd;
import com.zerui.manager.web.vo.DeviceTouchPageVO;
import com.zerui.manager.web.vo.feign.DeviceInfoFeignVO;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 设备网关接口
 *
 * @author pyzhang
 * @since 2024-04-11
 */
@Component
public class DeviceTouchRepository {
    @Resource
    private DeviceTouchGateWay deviceTouchGateway;

    public List<DeviceTouch> selectTouchDevicesByFrom(Integer deviceFrom) {
        return deviceTouchGateway.selectTouchDevicesByFrom(deviceFrom);
    }

    public Page<DeviceTouch> getPageDeviceList(DeviceTouchQryCmd qryCmd) {
        return deviceTouchGateway.getPageList(qryCmd);
    }

    public Page<DeviceTouchPageVO> getPageDeviceList2(DeviceTouchQryCmd qryCmd) {
        return deviceTouchGateway.getPageList2(qryCmd);
    }

    public List<DeviceInfoFeignVO> getAccessControlDeviceList(DeviceTouchQryCmd qryCmd) {
        return deviceTouchGateway.getAccessControlDeviceList(qryCmd);
    }

    @Transactional(rollbackFor = Exception.class)
    public void handSyncTouchDevices(List<DeviceTouch> remoteDevices, DeviceTouchSync touchSync) {
        List<DeviceTouch> touchedDevices = deviceTouchGateway.selectTouchDevicesByFrom(touchSync.getSyncFrom()); // 已落地的设备列表
        List<DeviceTouch> addDevices = new ArrayList<>(); // 新增设备
        List<DeviceTouch> updateDevices = new ArrayList<>(); // 更新设备
        List<DeviceTouch> deleteDevices = new ArrayList<>(); // 删除设备
        // 筛选出要更新和新增的设备
        for (DeviceTouch curr : remoteDevices) { // 遍历旧的数据
            Optional<DeviceTouch> exist = touchedDevices.stream().filter(c -> c.getDeviceId().equals(curr.getDeviceId())).findFirst();
            if (exist.isPresent()) {
                updateDevices.add(updateBuild(curr, exist.get()));
            } else {
                addDevices.add(curr);
            }
        }
        // 筛选出要删除的设备
        for (DeviceTouch exist : touchedDevices) {
            Optional<DeviceTouch> curr = remoteDevices.stream().filter(c -> c.getDeviceId().equals(exist.getDeviceId())).findFirst();
            if (!curr.isPresent()) {
                deleteDevices.add(exist);
            }
        }
        // 构建新增、修改、删除的设备
        touchSync.setSyncFromData(JSONObject.toJSONString(remoteDevices));
        touchSync.setSyncAddData(JSONObject.toJSONString(addDevices));
        touchSync.setSyncUpdateData(JSONObject.toJSONString(updateDevices));
        touchSync.setSyncDeleteData(JSONObject.toJSONString(deleteDevices));
        touchSync.setSyncRemark("本次任务新增【" + addDevices.size() + "】、更新【" + updateDevices.size() + "】、删除【" + deleteDevices.size() + "】数据.");
        deviceTouchGateway.handTouchDevices(addDevices, updateDevices, deleteDevices, touchSync);
    }

    /**
     * 同步落体设备信息
     *
     * @param remoteDevices 要要落地的设备信息
     * @param deviceFrom    设备源
     */
    @Transactional(rollbackFor = Exception.class)
    public void autoSyncTouchDevices(List<DeviceTouch> remoteDevices, Integer deviceFrom) {
        List<DeviceTouch> touchedDevices = deviceTouchGateway.selectTouchDevicesByFrom(deviceFrom); // 已落地的设备列表
        List<DeviceTouch> addDevices = new ArrayList<>(); // 新增的设备
        List<DeviceTouch> updateDevices = new ArrayList<>(); // 更新的设备
        List<DeviceTouch> deleteDevices = new ArrayList<>(); // 删除的设备
        // 筛选出要更新和新增的设备
        for (DeviceTouch curr : remoteDevices) { // 遍历旧的数据
            Optional<DeviceTouch> exist = touchedDevices.stream().filter(c -> c.getDeviceId().equals(curr.getDeviceId())).findFirst();
            if (exist.isPresent()) {
                updateDevices.add(updateBuild(curr, exist.get()));
            } else {
                addDevices.add(curr);
            }
        }
        // 筛选出要删除的设备
        for (DeviceTouch exist : touchedDevices) {
            Optional<DeviceTouch> curr = remoteDevices.stream().filter(c -> c.getDeviceId().equals(exist.getDeviceId())).findFirst();
            if (!curr.isPresent()) {
                deleteDevices.add(exist);
            }
        }
        deviceTouchGateway.autoTouchDevices(addDevices, updateDevices, deleteDevices);
    }

    private DeviceTouch updateBuild(DeviceTouch resource, DeviceTouch target) {
        target.setDeviceCode(resource.getDeviceCode());
        target.setDeviceName(resource.getDeviceName());
        target.setTypeCode(resource.getTypeCode());
        target.setTypeName(resource.getTypeName());
        target.setProductCode(resource.getProductCode());
        target.setProductName(resource.getProductName());
        target.setNodeType(resource.getNodeType());
        target.setNodeTypeName(resource.getNodeTypeName());
        target.setIsOnline(resource.getIsOnline()); // 1:在线
        target.setParentDeviceId(resource.getParentDeviceId());
        target.setParentDeviceName(resource.getParentDeviceName());
        target.setBuildNo(resource.getBuildNo());
        target.setBuildName(resource.getBuildName());
        target.setLevelNo(resource.getLevelNo());
        target.setLevelName(resource.getLevelName());
        target.setRoomNo(resource.getRoomNo());
        target.setRoomName(resource.getRoomName());
        return target;
    }

    public DeviceTouchPageVO getDeviceByDeviceCode(String deviceCode) {
        return deviceTouchGateway.getDeviceByDeviceCode(deviceCode);
    }
}